<?php

namespace app\api\controller\exam;

use app\admin\model\exam\MainCateModel;
use app\admin\model\exam\MainQuestionModel;
use app\admin\model\exam\QuestionModel;
use app\api\model\ExamPaperAssignment;
use app\common\controller\Api;
use app\common\controller\Backend;

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Session;

/**
 * 试题
 * @icon fa fa-circle-o
 */
class Assignment extends Api
{

    /**
     * QuestionModel模型对象
     *
     */
    protected $model = null;
    protected $noNeedRight = ['*'];
    protected $multiFields = 'status';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ExamPaperAssignment();


    }

    /**
     * Notes:获取作业列表
     *
     * DateTime: 2024/7/16 16:16
     */
    public function index()
    {
        $atype = $this->request->post('atype/d', 0);
        $userinfo = $this->auth->getUserinfo();
        $sort = 'id';
        $order = 'DESC';
        $offset = 1;
        $limit = 10;
        $profile_id = Db::name('classes_teacher')->where('user_id', $userinfo['user_id'])->value('profile_id');

        $where['type']=$atype;
        $where['profile_id'] = $profile_id;

        $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);

        $rows = $list->toArray()['data'];

        foreach ($rows as $key=>$row){
            $rows[$key]['createtime_format']=date("Y-m-d H:i:s",$row['createtime']);
            $rows[$key]['end_time_format']=date("Y-m-d H:i:s",$row['end_time']);
        }
        $result = array("total" => $list->total(), "rows" => $rows);
        return $this->success('成功',$result);
    }

    /**
     * Notes:创建课堂作业
     * DateTime: 2024/7/16 16:24
     */
    public function addClass()
    {
        $id = $this->request->post('id/d', 0);
        $title = $this->request->post('title/s', '');
        $limit_count = $this->request->post('limit_count/d', 0);
        $limit_time = $this->request->post('limit_time/d', 0);
        $end_time = $this->request->post('end_time/s', 0);
        $class_id = $this->request->post('class_id/d', 0);
        $papersModel = new \app\admin\model\exam\Papers();
        $userinfo = $this->auth->getUserinfo();
        $profile_id = Db::name('classes_teacher')->where('user_id', $userinfo['user_id'])->value('profile_id');
        if (empty($title)) {
            return $this->error('作业名称不能为空');
        }
        if (empty($class_id)) {
            return $this->error('请选择班级');
        }
        if (empty($id)) {//创建课堂作业
            if (empty($limit_time)) {
                return $this->error('请选择答题时长');
            }
            $type = 1;
        } else {//创建课后
            if (null == $parper = $papersModel->find($id)) {
                return $this->error('选择作业库不存在');
            }
            if (!empty($parper->deletetime)) {
                return $this->error('选择作业库已删除');
            }
            if (!in_array($profile_id,explode(',',$parper->share_ids))) {
                return $this->error('没有权利');
            }
            if (empty($end_time)) {
                return $this->error('请选择结束时间');
            }
            if (empty($limit_count)) {
                return $this->error('请选择允许答题次数');
            }
            $type = 0;
            $data['papers_id'] = $id;
            $data['type'] = $type;
            $data['title'] = $title;

            $data['quantity'] = count(explode(',',$parper->question_ids));
            $data['limit_time'] = 0;
            $data['limit_count'] = $limit_count;
            $data['join_count'] = 0;
            $data['start_time'] = 0;
            $data['end_time'] = strtotime($end_time);
            $data['status'] = 0;
            $data['classs_id'] = $class_id;
            $data['profile_id'] = $profile_id;
            $this->model->save($data);
            return $this->success('添加成功');


        }


    }

//    /**
//     * Notes:创建课后作业
//     * DateTime: 2024/7/16 16:24
//     */
//    public function addAfterClass(){
//        $id=$this->request->post('id/d',0);
//
//        $type=0;
//    }

}
